package com.shishuo.cms.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.shishuo.cms.constant.ExceptionConstant;
import com.shishuo.cms.constant.StatusConstant;
import com.shishuo.cms.dao.CashDao;
import com.shishuo.cms.entity.CashEntity;
import com.shishuo.cms.entity.UserEntity;
import com.shishuo.cms.entity.vo.PageVo;

@Service
public class CashService extends BaseService<CashEntity, Long>
{
	private CashDao cashDao;

	@Autowired
	private UserService userService;
	@Autowired
	private ConfigService configService;
	@Autowired
	public void setCashDao(CashDao cashDao)
	{
		super.setBaseDao(cashDao);
		this.cashDao = cashDao;
	}

	public PageVo<CashEntity> getAllListPage(int pageNum, long userId) throws Exception
	{
		PageVo<CashEntity> pageVo = new PageVo<CashEntity>(pageNum);
		List<CashEntity> list = cashDao.getAllList(pageVo.getOffset(), pageVo.getRows(), userId);
		pageVo.setList(list);
		pageVo.setCount(cashDao.getAllListCount(userId));
		return pageVo;
	}

	/**
	 * 用户充值提交
	 * 
	 * @param id
	 * @param inputMoney
	 */
	public void inputMoney(long userId, double inputMoney, long passUserId, String cashMsg)
	{
		CashEntity cashEntity = new CashEntity();
		cashEntity.setUserId(userId);
		cashEntity.setPassUserId(passUserId);
		cashEntity.setCashCount(inputMoney);
		cashEntity.setCashType(StatusConstant.CASH_TYPE_INPUTMONEY);
		cashEntity.setCashMsg("用户充值!流水号：" + cashMsg);
		cashDao.add(cashEntity);
	}
	//用户返现获取现金
	public void addMoneyByCallBack(long userId, double inputMoney, long passUserId, String cashMsg)
	{
		CashEntity cashEntity = new CashEntity();
		cashEntity.setUserId(userId);
		cashEntity.setPassUserId(passUserId);
		cashEntity.setCashCount(inputMoney);
		cashEntity.setStatus(1);//默认全部通过
		cashEntity.setCashType(StatusConstant.CASH_TYPE_INPUTMONEY);
		cashEntity.setCashMsg("用户获得返现!返现订单号为：" + cashMsg);
		userService.addUserMoney(userId, inputMoney);
		cashDao.add(cashEntity);
	}
	
	public void withdraw(double outputMoney, long userId, long passUserId, String cashMsg) throws Exception
	{
		UserEntity userEntity = userService.getById(userId);
		if (userEntity.getUserMoney() < outputMoney)
		{
			throw new Exception(ExceptionConstant.WITHDRAW_ERROR_USER_MONEY_NOT_ENOUGH);
		}
		CashEntity cashEntity = new CashEntity();
		cashEntity.setUserId(userId);
		cashEntity.setCashCount(outputMoney);
		cashEntity.setPassUserId(passUserId);
		cashEntity.setCashMsg("用户提现!"+cashMsg);
		cashEntity.setCashType(StatusConstant.CASH_TYPE_OUTPUTMONEY);
		cashDao.add(cashEntity);
		//用户可用金额减少，冻结金额增加
		userService.addUserMoney(userId, 0 - outputMoney);
		userService.addUserForzenMoney(userId, outputMoney);
	}

	public PageVo<CashEntity> getAllListPage(int pageNum)
	{
		PageVo<CashEntity> pageVo = new PageVo<CashEntity>(pageNum);
		List<CashEntity> list = cashDao.getAllList(pageVo.getOffset(), pageVo.getRows(), 0);
		pageVo.setList(list);
		pageVo.setCount(cashDao.getAllListCount(0));
		return pageVo;
	}

	//管理员处理提现信息
	public void withDrawByAdmin(CashEntity cashEntityOld) throws Exception
	{
		cashEntityOld.setLastUpdateDate(new Date());
		this.update(cashEntityOld);
		if (cashEntityOld.getStatus() == 1)
		{
			//用户冻结金额减少
			userService.addUserForzenMoney(cashEntityOld.getUserId(), 0 - cashEntityOld.getCashCount());
		}
		else if (cashEntityOld.getStatus() == -1)
		{
			userService.addUserForzenMoney(cashEntityOld.getUserId(), 0 - cashEntityOld.getCashCount());
			userService.addUserMoney(cashEntityOld.getUserId(), cashEntityOld.getCashCount());//返回到可用金额去
		}
	}

	public PageVo<CashEntity> getAllListPageByPassUserId(int pageNum, long passUserId)
	{
		PageVo<CashEntity> pageVo = new PageVo<CashEntity>(pageNum);
		List<CashEntity> list = cashDao.getAllListByPassUserId(pageVo.getOffset(), pageVo.getRows(), passUserId);
		pageVo.setList(list);
		pageVo.setCount(cashDao.getAllListCountByPassUserId(passUserId));
		return pageVo;
	}

	/**
	 * 处理充值提现
	 * 
	 * @throws Exception
	 */
	@Transactional(rollbackFor=java.lang.Exception.class) 
	public void processCashByUser(long loginUserId, long cashId,String passMsg) throws Exception
	{
		double inputMoneyCenterPercent=Double.valueOf(configService.getConfigByKey("inputMoneyCenterPercent")==""?"0":configService.getConfigByKey("inputMoneyCenterPercent"));
		double outputMoneyCenterPercent=Double.valueOf(configService.getConfigByKey("outputMoneyCenterPercent")==""?"0":configService.getConfigByKey("outputMoneyCenterPercent"));
		
		CashEntity cashEntity = this.getById(cashId);
		UserEntity userEntity = userService.getById(loginUserId);
		if ((null != cashEntity) && (userEntity != null))
		{
			if (cashEntity.getStatus()!=0) {
				throw new Exception(ExceptionConstant.CASH_PROCESS_ERROR_CASH_HAS_PROCESSED);
			}
			if (userEntity.getId() != cashEntity.getPassUserId())
			{
				throw new Exception(ExceptionConstant.CASH_PROCESS_ERROR_USER_IS_NOT_PERMITED);
			}
			else
			{
				cashEntity.setLastUpdateDate(new Date());
				cashEntity.setStatus(1);
				cashEntity.setPassMsg(passMsg);
				//根据类型不同进入不同的处理【充值提现兑换】
				switch (cashEntity.getCashType())
				{
					case 1://充值处理，当前登陆用户金额要减少，要充值的人资金要增加
						if (userEntity.getUserMoney()<cashEntity.getCashCount())
						{
							throw new Exception(ExceptionConstant.CASH_PROCESS_ERROR_USER_IS_NOT_ENOUGH_MONEY);
						}
						userService.addUserMoney(cashEntity.getUserId(), cashEntity.getCashCount());
						userService.addUserMoney(loginUserId, 0-cashEntity.getCashCount());
						addCashEntityToDb(StatusConstant.CASH_TYPE_INPUTMONEY,loginUserId,"充值审核",cashEntity.getCashCount(),"充值审核完毕扣除现金完毕！");
						userService.addUserMoney(loginUserId, cashEntity.getCashCount()*inputMoneyCenterPercent/100);
						addCashEntityToDb(StatusConstant.CASH_TYPE_INPUTMONEY,loginUserId,"充值审核赚取手续费",cashEntity.getCashCount()*inputMoneyCenterPercent/100,"充值审核完毕赚取手续费！");
						break;
					case 2://提现处理，当前登陆用户金额要增加，要提现的人冻结资金要减少
						userService.addUserForzenMoney(cashEntity.getUserId(), 0 - cashEntity.getCashCount());
						userService.addUserMoney(loginUserId, cashEntity.getCashCount());
						addCashEntityToDb(StatusConstant.CASH_TYPE_OUTPUTMONEY,loginUserId,"提现审核",cashEntity.getCashCount(),"提现审核完毕获取现金完毕！");
						userService.addUserMoney(loginUserId, cashEntity.getCashCount()*outputMoneyCenterPercent/100);
						addCashEntityToDb(StatusConstant.CASH_TYPE_INPUTMONEY,loginUserId,"提现审核赚取手续费",cashEntity.getCashCount()*outputMoneyCenterPercent/100,"提现审核完毕赚取手续费！");
						break;
					default:
						throw new Exception(ExceptionConstant.CASH_PROCESS_ERROR_NOT_CORRECT_TYPE);
						//break;
				}
				update(cashEntity);
			}
		}
		else
		{
			throw new Exception(ExceptionConstant.CASH_PROCESS_ERROR_CASH_NOT_FOUND);
		}
	}
	private void addCashEntityToDb(int cashType,long userId,String cashMsg,double cashCount,String passMsg)
	{
		CashEntity cashEntity = new CashEntity();
		cashEntity.setUserId(userId);
		cashEntity.setCashCount(cashCount);
		cashEntity.setCashMsg(cashMsg);
		cashEntity.setCashType(cashType);
		cashEntity.setPassMsg(passMsg);
		cashEntity.setStatus(1);//默认全部通过
		cashDao.add(cashEntity);
	}
}
